<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="margin-bottom">
      <!-- 搜索 -->
      <el-input
        clearable
        placeholder="输入名称搜索"
        style="width: 200px;"
        size="mini"
        class="filter-item"
      />
      <el-button
        class="filter-item"
        size="mini"
        type="success"
        icon="el-icon-search"
      >搜索</el-button>
      <!-- 新增 -->
      <div style="float:right">
        <el-button
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="add"
        >新增</el-button>
      </div>
    </div>
    <!--表单组件-->
    <eForm ref="form" :title="title" :is-add="isAdd"/>
    <!--表格渲染-->
    <el-table
      v-loading="loading"
      :data="menus"
      border
      row-key="uuid"
      style="width: 100%;"
    >
      <el-table-column prop="name" label="菜单名称" align="center"/>
      <el-table-column prop="icon" label="图标" align="center">
        <template slot-scope="scope">
          <svg-icon :icon-class="scope.row.icon"/>
        </template>
      </el-table-column>
      <el-table-column prop="sort" align="center" label="排序">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.sort }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="hidden" align="center" label="是否隐藏">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.hidden?'是':'否' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="path" align="center" label="链接地址"/>
      <el-table-column :show-overflow-tooltip="true" prop="template" align="center" label="组件路径"/>
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-plus" @click="append(scope.row)"/>
          <el-button size="mini" type="primary" icon="el-icon-edit" style="margin:0" @click="edit(scope.row)"/>
          <el-popover v-if="!scope.row.children" :ref="scope.row.uuid" placement="top" width="200">
            <p>确定删除该节点吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.uuid].doClose()">取消</el-button>
              <el-button
                :loading="delLoading"
                type="primary"
                size="mini"
                @click="remove(scope.row.uuid)"
              >确定</el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import eForm from './form';
import { menu_all, menu_remove } from '@/api/system';
import Menu from '@/utils/menu';
export default {
  components: { eForm },
  data() {
    return {
      menus: [],
      loading: false,
      delLoading: false,
      isAdd: false,
      title: ''
    };
  },
  created() {
    this.getMenus();
  },
  methods: {
    async getMenus() {
      this.loading = true;
      const res = await menu_all();
      this.menus = Menu.convertTree(res.data);
      this.loading = false;
    },
    add() {
      this.isAdd = true;
      this.title = '新增菜单';
      const _this = this.$refs.form;
      _this.isRoot = false;
      _this.resetForm();
      _this.dialog = true;
    },
    append(data) {
      this.isAdd = true;
      this.title = '新增子菜单';
      const _this = this.$refs.form;
      _this.resetForm();
      _this.dialog = true;
      _this.isRoot = true;
      _this.form.parentUuid = data.uuid;
    },
    edit(data) {
      this.isAdd = false;
      this.title = `编辑${data.name}`;
      const _this = this.$refs.form;
      if (data.parentUuid) {
        _this.isRoot = true;
      }
      _this.form = {
        uuid: data.uuid,
        parentUuid: data.parentUuid,
        hidden: data.hidden,
        name: data.name,
        sort: data.sort,
        path: data.path,
        template: data.template,
        icon: data.icon
      };
      _this.dialog = true;
    },
    async remove(id) {
      try {
        this.delLoading = true;
        await menu_remove(id);
        this.$refs[id].doClose();
        this.$message({
          message: '删除成功',
          type: 'success'
        });
        this.delLoading = false;
        this.getMenus();
      } catch (error) {
        this.delLoading = false;
        throw error;
      }
    }
  }
};
</script>
